
# OECD 

# Data preparation ----

gc()

## Set wd and load data
setwd("")
load("panel_ch_june_23.RData")

## Load packages
library(sjmisc)
library(tidyverse)
source("function_plot_agr.R")

# Take a glimpse, select variables of interest, remove labels, frequencies
glimpse(ch23)

df <- ch23 %>% select(vint_OCDE_w2, vchoice_OCDE_w2, particip_w2,
                      party_cue, pid_close_w1, pid_first_w1, pid_remind_w1, pid_remind_first_w1,
                      imp_OCDE_w2, complex_OCDE_w2,
                      genknow_a_w1, genknow_b_w1, genknow_c_w1, genknow_d_w1,  
                      arg_OCDE_a_w2, arg_OCDE_b_w2, arg_OCDE_c_w2,
                      arg_OCDE_d_w2, arg_OCDE_e_w2, arg_OCDE_f_w2, 
                      party_reco_OCDE_w2, gov_reco_OCDE_w2, 
                      intgrp_ecosuis_OCDE_w2, intgrp_usam_OCDE_w2,
                      intgrp_uss_OCDE_w2, day_itw_w2, month_itw_w2,
                      isknow_OCDE_a_w2, isknow_OCDE_b_w2, isknow_OCDE_c_w2,
                      starts_with("att"), -att_campaign_w3,
                      gender, age, educ_w1, region, received_w2, already_vote_w2)
                      
df <- labelled::remove_labels(df)
rm(ch23_open)


## Vote intention/choice ----
frq(df$vint_OCDE_w2)
frq(df$vchoice_OCDE_w2)

df <- df %>%
  mutate(votew2 = case_when(
    vint_OCDE_w2 == "certainly favor" | vint_OCDE_w2 == "likely favor" | vchoice_OCDE_w2 == "yes" ~ "yes",
    vint_OCDE_w2 == "certainly against" | vint_OCDE_w2 == "likely against" | vchoice_OCDE_w2 == "no" ~ "no",
    vint_OCDE_w2 == "dk" | vchoice_OCDE_w2 == "dk" ~ "dk"),
         vote_w2_int_choice = case_when(
    vint_OCDE_w2 == "certainly favor" | vint_OCDE_w2 == "likely favor" ~ "int yes",
    vchoice_OCDE_w2 == "yes" ~ "choice yes",
    vint_OCDE_w2 == "certainly against" | vint_OCDE_w2 == "likely against" ~ "int no",
    vchoice_OCDE_w2 == "no" ~ "choice no",
    vint_OCDE_w2 == "dk" | vchoice_OCDE_w2 == "dk" ~ "dk"))

frq(df$votew2)
xtabs(~ vint_OCDE_w2 + votew2, data = df, subset = NULL)
xtabs(~ vchoice_OCDE_w2 + votew2, data = df, subset = NULL)

frq(df$vote_w2_int_choice)
xtabs(~ vint_OCDE_w2 + vote_w2_int_choice, data = df, subset = NULL)
xtabs(~ vchoice_OCDE_w2 + vote_w2_int_choice, data = df, subset = NULL)





## Merge day / week of itw ----
frq(df$day_itw_w2)
frq(df$month_itw_w2)

xtabs(~ day_itw_w2 + month_itw_w2, data = df, subset = NULL)

df <- df %>% mutate(date_itw = paste(as.character(day_itw_w2), as.character(month_itw_w2), "2023", sep = "-"))

df$date_itw <- as.Date(df$date_itw, format = "%d-%m-%Y")

frq(df$date_itw)

df <- df %>% mutate(days = 41-(as.numeric(difftime(as.Date("2023-06-18"), date_itw, units = "days"))))

frq(df$days)
xtabs(~ date_itw + days, data = df, subset = NULL)


### Number of observations / each day 
freq_table <- as.data.frame(table(df$days, useNA = "no"))
N_values <- freq_table$Freq
mean(N_values)
sd(N_values)

rm(N_values, freq_table)




### Already voted or not
xtabs(~ already_vote_w2 + received_w2, data = df, addNA = T, subset = NULL)

df <- df %>%
  mutate(received_w2 = case_when(
    received_w2 == 1 ~ "yes",
    received_w2 == 2 ~ "no",
    received_w2 == 99 ~ "nsp",
    TRUE ~ NA), 
    already_vote_w2 = case_when(
      already_vote_w2 == 1 ~ "yes",
      already_vote_w2 == 2 ~ "no",
      TRUE ~ NA),
    votemail = case_when(
      received_w2 %in% c("no","nsp") ~ "not receive",
      received_w2 == "yes" & already_vote_w2 == "no" ~ "receive not vote",
      received_w2 == "yes" & already_vote_w2 == "yes" ~ "receive and vote",
      TRUE ~ NA))

xtabs(~ already_vote_w2 + received_w2, data = df, addNA = T, subset = NULL)
xtabs(~ already_vote_w2 + votemail, data = df, addNA = T, subset = NULL)
xtabs(~ votemail + received_w2, data = df, addNA = T, subset = NULL)
frq(df$votemail)
xtabs(~ votemail + vote_w2_int_choice, data = df, addNA = T, subset = NULL)

df <- df %>% mutate(filter_particip_profile = 
                      case_when(votemail == "receive and vote" | particip_w2 == "certainly yes" ~ 1,
                                is.na(days) ~ NA,
                                TRUE ~ 0))
frq(df$filter_particip_profile)
df %>% select(days, received_w2, already_vote_w2, particip_w2, votemail, vote_w2_int_choice, votew2, filter_particip_profile) %>% print(n = 50)



## Arguments ----
col_names <- c("arg_OCDE_a_w2", "arg_OCDE_b_w2", "arg_OCDE_c_w2", "arg_OCDE_d_w2", "arg_OCDE_e_w2", "arg_OCDE_f_w2")

df <- df %>%
  mutate(across(c(arg_OCDE_a_w2, arg_OCDE_b_w2, arg_OCDE_c_w2),
                  ~ case_when(. == "strong agree" ~ 2, . == "agree" ~ 1,
                              . == "dk" ~ 0,
                              . == "disagree" ~ -1, . == "strong disagree" ~ -2),
                              .names = "{col}_rcd")) %>% 
  mutate(across(c(arg_OCDE_d_w2, arg_OCDE_e_w2, arg_OCDE_f_w2),
                  ~ case_when(. == "strong agree" ~ -2, . == "agree" ~ -1,
                              . == "dk" ~ 0,
                              . == "disagree" ~ 1, . == "strong disagree" ~ 2),
                              .names = "{col}_rcd")) %>% 
  mutate(arg_pro_OCDE_w2 = arg_OCDE_a_w2_rcd + arg_OCDE_b_w2_rcd + arg_OCDE_c_w2_rcd,
         arg_con_OCDE_w2 = arg_OCDE_d_w2_rcd + arg_OCDE_e_w2_rcd + arg_OCDE_f_w2_rcd,
         arg_full_OCDE_w2 = arg_pro_OCDE_w2 + arg_con_OCDE_w2) %>% 
  mutate(arg_n_dk = case_when(rowSums(!is.na(across(all_of(col_names)))) == 0 ~ NA_real_,
                              T ~ rowSums(across(all_of(col_names)) == "dk", na.rm = T)))

rm(col_names)


## PID ----
df %>% select(starts_with("pid")) %>% frq()
xtabs(~ pid_close_w1 + pid_first_w1, data = df, subset = NULL)
xtabs(~ pid_close_w1 + pid_remind_w1, data = df, subset = NULL)
xtabs(~ pid_close_w1 + pid_remind_first_w1, data = df, subset = NULL)
xtabs(~ pid_remind_w1 + pid_remind_first_w1, data = df, subset = NULL)
xtabs(~ pid_first_w1 + pid_remind_first_w1, data = df, subset = NULL)


df <- df %>% mutate(pid = 
                      case_when(pid_first_w1 == "PLR" | pid_remind_first_w1 == "PLR" ~ "PLR",
                                pid_first_w1 == "Le Centre" | pid_remind_first_w1 == "Le Centre" ~ "Le Centre",
                                pid_first_w1 == "PS" | pid_remind_first_w1 == "PS" ~ "PS",
                                pid_first_w1 == "UDC" | pid_remind_first_w1 == "UDC" ~ "UDC",
                                pid_first_w1 == "PES" | pid_remind_first_w1 == "PES" ~ "PES",
                                pid_first_w1 == "PVL" | pid_remind_first_w1 == "PVL" ~ "PVL",
                                pid_first_w1 %in% c("Lega","MCG","PCS","PEV","UDF","PST-POP") | pid_remind_first_w1 %in% c("Lega","MCG","PCS","PEV","UDF","PST-POP") ~ "Other",
                                pid_remind_first_w1 == "dk" ~ "noPID",
                                pid_close_w1 == "no" & (pid_remind_w1 == "no" | pid_remind_w1 == "dk") ~ "noPID",
                                pid_close_w1 == "dk" & (pid_remind_w1 == "no" | pid_remind_w1 == "dk") ~ "noPID",
                                pid_close_w1 == "yes" & pid_remind_w1 == "no" ~ "noPID"))

frq(df$pid)
df %>% select(starts_with("pid")) %>% print(n=50)


## Knowledge party cues ----
frq(df$pid)
df <- df %>% filter(pid %in% c("PLR","Le Centre","PS","UDC","PES","PVL","noPID"))
frq(df$pid)

frq(df$party_reco_OCDE_w2) 
xtabs(~ pid + party_reco_OCDE_w2, data = df, subset = NULL)
addmargins(xtabs(~ pid + party_reco_OCDE_w2, data = df, subset = NULL))

df <- df %>% 
  mutate(party_reco_OCDE_w2_know = case_when(
    pid %in% c("PLR","Le Centre","PVL","UDC") & party_reco_OCDE_w2 == "yes" ~ "correct",
    pid == "PS" & party_reco_OCDE_w2 == "no" ~ "correct",
    pid == "PES" & party_reco_OCDE_w2 == "free" ~ "correct",
    pid %in% c("PLR","Le Centre","PVL","UDC") & party_reco_OCDE_w2 %in% c("no","free","dk") ~ "not",
    pid == "PS" & party_reco_OCDE_w2 %in% c("yes","free","dk") ~ "not",
    pid == "PES" & party_reco_OCDE_w2 %in% c("yes","no","dk") ~ "not",
    pid == "noPID" ~ "noPID"))

frq(df$party_reco_OCDE_w2_know) 


# Group "free" with "dk" (knowledge of cues)
df <- df %>% 
  mutate(across(c(party_reco_OCDE_w2),
                    ~ case_when(. == "yes" ~ "yes",
                                . == "no" ~ "no",
                                . == "free" | . == "dk" ~ "free/dk"),
                                .names = "{col}_rcd"))

frq(df$party_reco_OCDE_w2_rcd)


## Importance ----
frq(df$imp_OCDE_w2)
df$imp_OCDE_w2[df$imp_OCDE_w2 == 99] <- NA
frq(df$imp_OCDE_w2)

df <- df %>% mutate(imp_OCDE_w2_bi = case_when(
  imp_OCDE_w2 < 6 ~ 0,
  imp_OCDE_w2 >= 6 ~ 1,
  TRUE ~ NA))  

xtabs(~ imp_OCDE_w2 + imp_OCDE_w2_bi, data = df, subset = NULL)



## IS knowledge ----
frq(df$isknow_OCDE_a_w2)
frq(df$isknow_OCDE_b_w2)
frq(df$isknow_OCDE_c_w2)

df <- df %>%
  mutate(isknow_OCDE_a_w2_rcd = ifelse(is.na(isknow_OCDE_a_w2), NA, as.numeric(grepl("correct", tolower(isknow_OCDE_a_w2)))),
         isknow_OCDE_b_w2_rcd = ifelse(is.na(isknow_OCDE_b_w2), NA, as.numeric(grepl("correct", tolower(isknow_OCDE_b_w2)))),
         isknow_OCDE_c_w2_rcd = ifelse(is.na(isknow_OCDE_c_w2), NA, as.numeric(grepl("correct", tolower(isknow_OCDE_c_w2)))))

frq(df$isknow_OCDE_a_w2_rcd)
frq(df$isknow_OCDE_b_w2_rcd)
frq(df$isknow_OCDE_c_w2_rcd)

df <- df %>% mutate(isknow_OCDE_scale = isknow_OCDE_a_w2_rcd + isknow_OCDE_b_w2_rcd + isknow_OCDE_c_w2_rcd) %>% 
             mutate(isknow_OCDE_scale_rcd = case_when(isknow_OCDE_scale == 0 ~ 0,
                                                        isknow_OCDE_scale == 1 ~ 1,
                                                        isknow_OCDE_scale == 2 | isknow_OCDE_scale == 3 ~ 2)) %>% 
             mutate(isknow_OCDE_scale_bi = case_when(isknow_OCDE_scale == 0 | isknow_OCDE_scale == 1 ~ 0,
                                                       isknow_OCDE_scale == 2 | isknow_OCDE_scale == 3 ~ 1))
             
frq(df$isknow_OCDE_scale)
frq(df$isknow_OCDE_scale_rcd)
frq(df$isknow_OCDE_scale_bi)



## Select var and remove NA ----


df <- df %>% 
  select(isknow_OCDE_scale, party_reco_OCDE_w2_know, arg_n_dk, arg_full_OCDE_w2, filter_particip_profile, 
         votemail, date_itw, days, votew2, pid, imp_OCDE_w2, gender, age, educ_w1,
         arg_OCDE_a_w2_rcd, arg_OCDE_b_w2_rcd, arg_OCDE_c_w2_rcd, arg_OCDE_d_w2_rcd, arg_OCDE_e_w2_rcd, arg_OCDE_f_w2_rcd) %>% 
  drop_na()



## Recode mediators/outcomes ----

### Consistent voting ----
df <- df %>% mutate(
  voteinlinearg = case_when(votew2 == "dk" ~ "undecided",
                            arg_full_OCDE_w2 <= -3 & votew2 == "no" ~ "inline",
                            arg_full_OCDE_w2 <= -3 & votew2 == "yes" ~ "not inline",
                            arg_full_OCDE_w2 >= -2 & arg_full_OCDE_w2 <= 2 ~ "ambivalent",
                            arg_full_OCDE_w2 >= 3 & votew2 == "no" ~ "not inline",
                            arg_full_OCDE_w2 >= 3 & votew2 == "yes" ~ "inline"),
  voteinlinearg0 = case_when(votew2 == "dk" ~ "undecided",
                             arg_full_OCDE_w2 <= -1 & votew2 == "no" ~ "inline",
                             arg_full_OCDE_w2 <= -1 & votew2 == "yes" ~ "not inline",
                             arg_full_OCDE_w2 == 0 ~ "ambivalent",
                             arg_full_OCDE_w2 >= 1 & votew2 == "no" ~ "not inline",
                             arg_full_OCDE_w2 >= 1 & votew2 == "yes" ~ "inline"),
  voteinlinearg5 = case_when(votew2 == "dk" ~ "undecided",
                             arg_full_OCDE_w2 <= -6 & votew2 == "no" ~ "inline",
                             arg_full_OCDE_w2 <= -6 & votew2 == "yes" ~ "not inline",
                             arg_full_OCDE_w2 >= -5 & arg_full_OCDE_w2 <= 5 ~ "ambivalent",
                             arg_full_OCDE_w2 >= 6 & votew2 == "no" ~ "not inline",
                             arg_full_OCDE_w2 >= 6 & votew2 == "yes" ~ "inline"),
  voteinlinearg_rcd = case_when(voteinlinearg == "inline" ~ 1,
                                voteinlinearg %in% c("ambivalent","not inline","undecided") ~ 0),
  voteinlinearg0_rcd = case_when(voteinlinearg0 == "inline" ~ 1,
                                 voteinlinearg0 %in% c("ambivalent","not inline","undecided") ~ 0),
  voteinlinearg5_rcd = case_when(voteinlinearg5 == "inline" ~ 1,
                                 voteinlinearg5 %in% c("ambivalent","not inline","undecided") ~ 0))


### Knowledge cue and vote inline ----
df <- df %>% mutate(party_reco_OCDE_w2_know_rcd = case_when(party_reco_OCDE_w2_know == "correct" ~ 1,
                                                            party_reco_OCDE_w2_know == "noPID" | party_reco_OCDE_w2_know == "not" ~ 0),
                    voteinlineparty = case_when(pid %in% c("PLR","Le Centre","PVL","UDC") & votew2 == "yes" ~ "inline",
                                                pid %in% c("PLR","Le Centre","PVL","UDC") & votew2 == "no" ~ "not inline",
                                                pid %in% c("PLR","Le Centre","PVL","UDC") & votew2 == "dk" ~ "undecided",
                                                pid == "PS" & votew2 == "no" ~ "inline",
                                                pid == "PS" & votew2 == "yes" ~ "not inline",
                                                pid == "PS" & votew2 == "dk" ~ "undecided",
                                                pid == "PES" & votew2 == "no" ~ "inline",
                                                pid == "PES" & votew2 == "yes" ~ "inline",
                                                pid == "PES" & votew2 == "dk" ~ "undecided",
                                                pid == "noPID" ~ "noPID"),
                    voteinlineparty_rcd = case_when(voteinlineparty == "inline" ~ 1,
                                                    voteinlineparty %in% c("not inline", "undecided", "noPID") ~ 0))

frq(df$voteinlineparty)


## Remove 18th June (8 obs) ----
df <- df %>% filter(date_itw != "2023-06-18")

## Filter only party identifiers ---- 
df <- df %>% filter(pid != "noPID")
frq(df$pid)


# Descriptive / appendix A ----

frq(df$votemail)

frq(df$party_reco_OCDE_w2_know_rcd)

ggplot(df) + 
  aes(x = arg_full_OCDE_w2) +
  geom_density(fill = "grey", alpha = 0.5) +
  theme_minimal() +
  scale_x_continuous(limits = c(-12, 12), breaks = seq(-12, 12, by = 2)) + 
  xlab("OECD - argument scale")  +
  ylab("Density")

ggsave("scale_OCDE.png", scale = 1, width = 12, height = 6, units = "cm", dpi = 600)

avg_dk_OCDE <- mean(df$arg_n_dk)
round(avg_dk_OCDE,2)
sd_dk_OCDE <- sd(df$arg_n_dk)
round(sd_dk_OCDE,2)

frq(df$voteinlineparty_rcd)
frq(df$voteinlinearg_rcd)

addmargins(xtabs(~ voteinlineparty_rcd + voteinlinearg_rcd, data = df, subset = NULL))
cross_OCDE <- as.data.frame(questionr::prop(xtabs(~ voteinlineparty_rcd + voteinlinearg_rcd, data = df, subset = NULL)))
cross_OCDE <- cross_OCDE %>% mutate(ballot = "OECD")
cross_OCDE

rm(avg_dk_OCDE, sd_dk_OCDE)


# Alpha arguments
df %>% 
  select(arg_OCDE_a_w2_rcd, arg_OCDE_b_w2_rcd, arg_OCDE_c_w2_rcd) %>% 
  psych::alpha()  

df %>% 
  select(arg_OCDE_d_w2_rcd, arg_OCDE_e_w2_rcd, arg_OCDE_f_w2_rcd) %>% 
  psych::alpha() 



# Analysis - aggregate ----

## 2a ----
cat_ols(df, "party_reco_OCDE_w2_know_rcd", ballot = "OCDE")

p01_OCDE <- graf

p01_OCDE <- p01_OCDE +
  geom_point(shape = 16, size = 1, alpha = 0.75) +
  geom_smooth(aes(y = percentage), method = "lm", alpha = 0.2, linewidth = 0.5) +
  labs(x = "Days before voting day", y = "Percentage", color = "", fill = "", title = "OECD") +
  theme_minimal() +
  theme(legend.position = "bottom") +
  scale_color_manual(breaks = c("0","1"), labels = c("Not correct","Correct"),
                                                       values = c("0" = "grey70", "1" = "grey30")) +
  scale_fill_manual(breaks = c("0","1"), labels = c("Not correct","Correct"),
                    values = c("0" = "grey70", "1" = "grey30")) +
  scale_y_continuous(breaks = seq(0, 100, by = 20), limits = c(0,100)) +
  scale_x_continuous(breaks = c(1, 11, 21, 31, 40), labels = c("-40", "-30", "-20", "-10", "0"))

p01_OCDE

magr <- lm(percentage ~ days * party_reco_OCDE_w2_know_rcd, data = dfagr)
summary(magr)


magr_know_cue <- broom::tidy(magr) %>% select(term, estimate, std.error, p.value) %>%
  mutate(term = recode(term,
                       "(Intercept)" = "Intercept", "days" = "Days",
                       "party_reco_OCDE_w2_know_rcd1" = "Knowledge party cue",
                       "days:party_reco_OCDE_w2_know_rcd1" = "Days × Knowledge party cue"),
         estimate  = round(estimate, 2),
         std.error = round(std.error, 2),
         sig       = ifelse(p.value < 0.05, "p<0.05", "ns")) %>%
  select(-p.value) %>%
  rename(Variable = term, Coefficient = estimate, SE = std.error, Sig = sig) 

## 2b ----
frq(df$arg_n_dk)

num_ols(df, "arg_n_dk", ballot = "OCDE")
p02_OCDE <- graf

p02_OCDE <- p02_OCDE +
  geom_point(shape = 16, size = 1, alpha = 0.75) +
  geom_smooth(aes(y = avg), method = "lm", alpha = 0.2, linewidth = 0.5, color = "black") +
  labs(x = "Days before voting day", y = "Average", color = "", fill = "", title = "OECD") +
  theme_minimal() +
  scale_y_continuous(breaks = seq(0, 2, by = 0.2), limits = c(0,2)) +
  scale_x_continuous(breaks = c(1, 11, 21, 31, 40), labels = c("-40", "-30", "-20", "-10", "0"))

p02_OCDE

magr <- lm(avg ~ days, data = dfagr)
summary(magr)

magr_dk <- broom::tidy(magr) %>% select(term, estimate, std.error, p.value) %>%
  mutate(term = recode(term, "(Intercept)" = "Intercept", "days" = "Days"),
         estimate  = round(estimate, 2),
         std.error = round(std.error, 2),
         sig       = ifelse(p.value < 0.05, "p<0.05", "ns")) %>%
  select(-p.value) %>%
  rename(Variable = term, Coefficient = estimate, SE = std.error, Sig = sig)  



## 2c ----
frq(df$voteinlineparty_rcd)

cat_ols(df, "voteinlineparty_rcd", ballot = "OCDE")
p04_OCDE <- graf

p04_OCDE <- p04_OCDE +
  geom_point(shape = 16, size = 1, alpha = 0.75) +
  geom_smooth(aes(y = percentage), method = "lm", alpha = 0.2, linewidth = 0.5) +
  labs(x = "Days before voting day", y = "Percentage", color = "", fill = "",
       title = "OECD") +
  theme_minimal() +
  theme(legend.position = "bottom") +
  scale_color_manual(breaks = c("0","1"), labels = c("Not inline","Inline"),
                     values = c("0" = "grey70", "1" = "grey30")) +
  scale_fill_manual(breaks = c("0","1"), labels = c("Not inline","Inline"),
                    values = c("0" = "grey70", "1" = "grey30")) +
  scale_y_continuous(breaks = seq(0, 100, by = 20), limits = c(0,100)) +
  scale_x_continuous(breaks = c(1, 11, 21, 31, 40), labels = c("-40", "-30", "-20", "-10", "0"))

p04_OCDE

magr <- lm(percentage ~ days * voteinlineparty_rcd, data = dfagr)
summary(magr)

magr_inline <- broom::tidy(magr) %>% select(term, estimate, std.error, p.value) %>%
  mutate(term = recode(term,
                       "(Intercept)" = "Intercept", "days" = "Days",
                       "voteinlineparty_rcd1" = "Inline",
                       "days:voteinlineparty_rcd1" = "Days × Inline"),
         estimate  = round(estimate, 2),
         std.error = round(std.error, 2),
         sig       = ifelse(p.value < 0.05, "p<0.05", "ns")) %>%
  select(-p.value) %>%
  rename(Variable = term, Coefficient = estimate, SE = std.error, Sig = sig) 



## 2d ----
frq(df$voteinlinearg_rcd)

cat_ols(df, "voteinlinearg_rcd", ballot = "OCDE")
p03_OCDE <- graf

p03_OCDE <- p03_OCDE +
  geom_point(shape = 16, size = 1, alpha = 0.75) +
  geom_smooth(aes(y = percentage), method = "lm", alpha = 0.2, linewidth = 0.5) +
  labs(x = "Days before voting day", y = "Percentage", color = "", fill = "",
       title = "OECD") +
  theme_minimal() +
  theme(legend.position = "bottom") +
  scale_color_manual(breaks = c("0","1"), labels = c("Not consistent","Consistent"),
                     values = c("0" = "grey70", "1" = "grey30")) +
  scale_fill_manual(breaks = c("0","1"), labels = c("Not consistent","Consistent"),
                    values = c("0" = "grey70", "1" = "grey30")) +
  scale_y_continuous(breaks = seq(0, 100, by = 20), limits = c(0,100)) +
  scale_x_continuous(breaks = c(1, 11, 21, 31, 40), labels = c("-40", "-30", "-20", "-10", "0"))

p03_OCDE

magr <- lm(percentage ~ days * voteinlinearg_rcd, data = dfagr)
summary(magr)

magr_cons <- broom::tidy(magr) %>% select(term, estimate, std.error, p.value) %>%
  mutate(term = recode(term,
                       "(Intercept)" = "Intercept", "days" = "Days",
                       "voteinlinearg_rcd1" = "Consistent voting",
                       "days:voteinlinearg_rcd1" = "Days × Consistent voting"),
         estimate  = round(estimate, 2),
         std.error = round(std.error, 2),
         sig       = ifelse(p.value < 0.05, "p<0.05", "ns")) %>%
  select(-p.value) %>%
  rename(Variable = term, Coefficient = estimate, SE = std.error, Sig = sig) 



## Appendix B1 ---- 

writexl::write_xlsx(list(
  "2a" = magr_know_cue, "2b" = magr_dk,
  "2c" = magr_inline, "2d" = magr_cons),
  "magr_ocde.xlsx")

#

rm(graf, magr, dfagr)



# Analysis - individual ---- 

glimpse(df)

colsss <- c("gender", "voteinlineparty_rcd", "voteinlinearg_rcd", "voteinlinearg0_rcd", "voteinlinearg5_rcd")
df[colsss] <- lapply(df[colsss], as.factor)

colsss <- c("party_reco_OCDE_w2_know_rcd", "isknow_OCDE_scale", "arg_n_dk", "days", "imp_OCDE_w2", "age", "educ_w1")
df[colsss] <- lapply(df[colsss], as.numeric)
df[colsss] <- lapply(df[colsss], scales::rescale)

rm(colsss)


## Appendix B2 ----

library(lavaan)

glimpse(df)

df$party_reco_OCDE_w2_know_rcd <- as.factor(df$party_reco_OCDE_w2_know_rcd)
df$gender <- as.numeric(df$gender)

model <- '
  # Days -> cue -> vote inline
  party_reco_OCDE_w2_know_rcd ~ a1*days + isknow_OCDE_scale + imp_OCDE_w2 + educ_w1 + gender + age  
  voteinlineparty_rcd ~ c1*days + b1*party_reco_OCDE_w2_know_rcd + isknow_OCDE_scale + imp_OCDE_w2 + educ_w1 + gender + age   

  # Days -> dk arg -> consistent
  arg_n_dk ~ a2*days + isknow_OCDE_scale + imp_OCDE_w2 + educ_w1 + gender + age  
  voteinlinearg_rcd ~ c2*days + b2*arg_n_dk + isknow_OCDE_scale + imp_OCDE_w2 + educ_w1 + gender + age  

  # Covariances
  party_reco_OCDE_w2_know_rcd ~~ arg_n_dk
  voteinlineparty_rcd ~~ voteinlinearg_rcd

  # Indirect
  days_cue_inline := a1*b1
  days_dk_cons := a2*b2
  
  # Total
  tot_inline := c1 + (a1*b1) 
  tot_cons := c2 + (a2*b2) 
'


fit_OCDE <- sem(model, data = df, ordered = c("voteinlineparty_rcd","voteinlinearg_rcd","party_reco_OCDE_w2_know_rcd"))

sumfit_OCDE <- summary(fit_OCDE, fit.measures = T, standardized = F, rsquare = T, ci = T)
sumfit_OCDE_df <- sumfit_OCDE[["pe"]]




# Robustness ----

## Model for figure C3a.3 ----

model0 <- '
  # Days -> cue -> vote inline
  party_reco_OCDE_w2_know_rcd ~ a1*days + isknow_OCDE_scale + imp_OCDE_w2 + educ_w1 + gender + age  
  voteinlineparty_rcd ~ c1*days + b1*party_reco_OCDE_w2_know_rcd + isknow_OCDE_scale + imp_OCDE_w2 + educ_w1 + gender + age   

  # Days -> dk arg -> consistent
  arg_n_dk ~ a2*days + isknow_OCDE_scale + imp_OCDE_w2 + educ_w1 + gender + age  
  voteinlinearg0_rcd ~ c2*days + b2*arg_n_dk + isknow_OCDE_scale + imp_OCDE_w2 + educ_w1 + gender + age  

  # Covariances
  party_reco_OCDE_w2_know_rcd ~~ arg_n_dk
  voteinlineparty_rcd ~~ voteinlinearg0_rcd

  # Indirect
  days_cue_inline := a1*b1
  days_dk_cons := a2*b2
  
  # Total
  tot_inline := c1 + (a1*b1) 
  tot_cons := c2 + (a2*b2) 
'

fit_OCDE0 <- sem(model0, data = df, ordered = c("voteinlineparty_rcd","voteinlinearg0_rcd","party_reco_OCDE_w2_know_rcd"))

sumfit_OCDE0 <- summary(fit_OCDE0, fit.measures = T, standardized = F, rsquare = T, ci = T)
sumfit_OCDE0_df <- sumfit_OCDE0[["pe"]]


## Model for figure C3b.3 ----

model5 <- '
  # Days -> cue -> vote inline
  party_reco_OCDE_w2_know_rcd ~ a1*days + isknow_OCDE_scale + imp_OCDE_w2 + educ_w1 + gender + age  
  voteinlineparty_rcd ~ c1*days + b1*party_reco_OCDE_w2_know_rcd + isknow_OCDE_scale + imp_OCDE_w2 + educ_w1 + gender + age   

  # Days -> dk arg -> consistent
  arg_n_dk ~ a2*days + isknow_OCDE_scale + imp_OCDE_w2 + educ_w1 + gender + age  
  voteinlinearg5_rcd ~ c2*days + b2*arg_n_dk + isknow_OCDE_scale + imp_OCDE_w2 + educ_w1 + gender + age  

  # Covariances
  party_reco_OCDE_w2_know_rcd ~~ arg_n_dk
  voteinlineparty_rcd ~~ voteinlinearg5_rcd

  # Indirect
  days_cue_inline := a1*b1
  days_dk_cons := a2*b2
  
  # Total
  tot_inline := c1 + (a1*b1) 
  tot_cons := c2 + (a2*b2) 
'

fit_OCDE5 <- sem(model5, data = df, ordered = c("voteinlineparty_rcd","voteinlinearg5_rcd","party_reco_OCDE_w2_know_rcd"))

sumfit_OCDE5 <- summary(fit_OCDE5, fit.measures = T, standardized = F, rsquare = T, ci = T)
sumfit_OCDE5_df <- sumfit_OCDE5[["pe"]]



## Figure C3a.2d ----

frq(df$voteinlinearg0_rcd)

cat_ols(df, "voteinlinearg0_rcd", ballot = "OECD")
pzero_OECD <- graf

pzero_OECD[["data"]][["days"]] <- pzero_OECD[["data"]][["days"]] * 40

pzero_OECD <- pzero_OECD +
  geom_point(shape = 16, size = 1, alpha = 0.75) +
  geom_smooth(aes(y = percentage), method = "lm", alpha = 0.2, linewidth = 0.5) +
  labs(x = "Days before voting day", y = "Percentage", color = "", fill = "",
       title = "OECD") +
  theme_minimal() +
  theme(legend.position = "bottom") +
  scale_color_manual(breaks = c("0","1"), labels = c("Not consistent","Consistent"),
                     values = c("0" = "grey70", "1" = "grey30")) +
  scale_fill_manual(breaks = c("0","1"), labels = c("Not consistent","Consistent"),
                    values = c("0" = "grey70", "1" = "grey30")) +
  scale_y_continuous(breaks = seq(0, 100, by = 20), limits = c(0,100)) +
  scale_x_continuous(breaks = c(1, 11, 21, 31, 40), labels = c("-40", "-30", "-20", "-10", "0"))

pzero_OECD

magr <- lm(percentage ~ days * voteinlinearg0_rcd, data = dfagr)
summary(magr)

rm(graf)



## Figure C3b.2d ----

frq(df$voteinlinearg5_rcd)

cat_ols(df, "voteinlinearg5_rcd", ballot = "OECD")
pfive_OECD <- graf

pfive_OECD[["data"]][["days"]] <- pfive_OECD[["data"]][["days"]] * 40


pfive_OECD <- pfive_OECD +
  geom_point(shape = 16, size = 1, alpha = 0.75) +
  geom_smooth(aes(y = percentage), method = "lm", alpha = 0.2, linewidth = 0.5) +
  labs(x = "Days before voting day", y = "Percentage", color = "", fill = "",
       title = "OECD") +
  theme_minimal() +
  theme(legend.position = "bottom") +
  scale_color_manual(breaks = c("0","1"), labels = c("Not consistent","Consistent"),
                     values = c("0" = "grey70", "1" = "grey30")) +
  scale_fill_manual(breaks = c("0","1"), labels = c("Not consistent","Consistent"),
                    values = c("0" = "grey70", "1" = "grey30")) +
  scale_y_continuous(breaks = seq(0, 100, by = 20), limits = c(0,100)) +
  scale_x_continuous(breaks = c(1, 11, 21, 31, 40), labels = c("-40", "-30", "-20", "-10", "0"))

pfive_OECD

magr <- lm(percentage ~ days * voteinlinearg5_rcd, data = dfagr)
summary(magr)

rm(graf)









## Appendix C1 ----

df_certain_voter <- df %>% filter(filter_particip_profile == 1)

### Figure C1.2a ----
cat_ols(df_certain_voter, "party_reco_OCDE_w2_know_rcd", ballot = "OECD")

pcertain01_OCDE <- graf

pcertain01_OCDE[["data"]][["days"]] <- pcertain01_OCDE[["data"]][["days"]] * 40


pcertain01_OCDE <- pcertain01_OCDE +
  geom_point(shape = 16, size = 1, alpha = 0.75) +
  geom_smooth(aes(y = percentage), method = "lm", alpha = 0.2, linewidth = 0.5) +
  labs(x = "Days before voting day", y = "Percentage", color = "", fill = "", 
       title = "OECD") +
  theme_minimal() +
  theme(legend.position = "bottom") +
  scale_color_manual(breaks = c("0","1"), labels = c("Not correct","Correct"),
                     values = c("0" = "grey70", "1" = "grey30")) +
  scale_fill_manual(breaks = c("0","1"), labels = c("Not correct","Correct"),
                    values = c("0" = "grey70", "1" = "grey30")) +
  scale_y_continuous(breaks = seq(0, 100, by = 20), limits = c(0,100)) +
  scale_x_continuous(breaks = c(1, 11, 21, 31, 40), labels = c("-40", "-30", "-20", "-10", "0"))

pcertain01_OCDE

magr <- lm(percentage ~ days * party_reco_OCDE_w2_know_rcd, data = dfagr)
summary(magr)


### Figure C1.2b ----
frq(df_certain_voter$arg_n_dk)

num_ols(df_certain_voter, "arg_n_dk", ballot = "OECD")
pcertain02_OCDE <- graf

pcertain02_OCDE[["data"]][["days"]] <- pcertain02_OCDE[["data"]][["days"]] * 40
pcertain02_OCDE[["data"]][["avg"]] <- pcertain02_OCDE[["data"]][["avg"]] * 6

pcertain02_OCDE <- pcertain02_OCDE +
  geom_point(shape = 16, size = 1, alpha = 0.75) +
  geom_smooth(aes(y = avg), method = "lm", alpha = 0.2, linewidth = 0.5, color = "black") +
  labs(x = "Days before voting day", y = "Average", color = "", fill = "",
       title = "OECD") +
  theme_minimal() +
  scale_y_continuous(breaks = seq(0, 2, by = 0.2), limits = c(0,2)) +
  scale_x_continuous(breaks = c(1, 11, 21, 31, 40), labels = c("-40", "-30", "-20", "-10", "0"))

pcertain02_OCDE

magr <- lm(avg ~ days, data = dfagr)
summary(magr)



### Figure C1.2c ----
frq(df_certain_voter$voteinlineparty_rcd)

cat_ols(df_certain_voter, "voteinlineparty_rcd", ballot = "OECD")
pcertain04_OCDE <- graf

pcertain04_OCDE[["data"]][["days"]] <- pcertain04_OCDE[["data"]][["days"]] * 40

pcertain04_OCDE <- pcertain04_OCDE +
  geom_point(shape = 16, size = 1, alpha = 0.75) +
  geom_smooth(aes(y = percentage), method = "lm", alpha = 0.2, linewidth = 0.5) +
  labs(x = "Days before voting day", y = "Percentage", color = "", fill = "",
       title = "OECD") +
  theme_minimal() +
  theme(legend.position = "bottom") +
  scale_color_manual(breaks = c("0","1"), labels = c("Not inline","Inline"),
                     values = c("0" = "grey70", "1" = "grey30")) +
  scale_fill_manual(breaks = c("0","1"), labels = c("Not inline","Inline"),
                    values = c("0" = "grey70", "1" = "grey30")) +
  scale_y_continuous(breaks = seq(0, 100, by = 20), limits = c(0,100)) +
  scale_x_continuous(breaks = c(1, 11, 21, 31, 40), labels = c("-40", "-30", "-20", "-10", "0"))

pcertain04_OCDE

magr <- lm(percentage ~ days * voteinlineparty_rcd, data = dfagr)
summary(magr)




### Figure C1.2d ----
frq(df_certain_voter$voteinlinearg_rcd)

cat_ols(df_certain_voter, "voteinlinearg_rcd", ballot = "OECD")
pcertain03_OCDE <- graf

pcertain03_OCDE[["data"]][["days"]] <- pcertain03_OCDE[["data"]][["days"]] * 40

pcertain03_OCDE <- pcertain03_OCDE +
  geom_point(shape = 16, size = 1, alpha = 0.75) +
  geom_smooth(aes(y = percentage), method = "lm", alpha = 0.2, linewidth = 0.5) +
  labs(x = "Days before voting day", y = "Percentage", color = "", fill = "",
       title = "OECD") +
  theme_minimal() +
  theme(legend.position = "bottom") +
  scale_color_manual(breaks = c("0","1"), labels = c("Not consistent","Consistent"),
                     values = c("0" = "grey70", "1" = "grey30")) +
  scale_fill_manual(breaks = c("0","1"), labels = c("Not consistent","Consistent"),
                    values = c("0" = "grey70", "1" = "grey30")) +
  scale_y_continuous(breaks = seq(0, 100, by = 20), limits = c(0,100)) +
  scale_x_continuous(breaks = c(1, 11, 21, 31, 40), labels = c("-40", "-30", "-20", "-10", "0"))

pcertain03_OCDE

magr <- lm(percentage ~ days * voteinlinearg_rcd, data = dfagr)
summary(magr)

rm(graf, magr, dfagr)



### Results for figure C1.3b ----
fit_OCDEcertain <- sem(model, data = df_certain_voter, ordered = c("voteinlineparty_rcd","voteinlinearg_rcd","party_reco_OCDE_w2_know_rcd"))

sumfit_OCDEcertain <- summary(fit_OCDEcertain, fit.measures = T, standardized = F, rsquare = T, ci = T)
sumfit_OCDEcertain_df <- sumfit_OCDEcertain[["pe"]]


rm(model, model0, model5)



